'use strict';

/**
 * Created by twer on 15/3/6.
 */
angular.module('photo')
    .factory('PhotoListService', [
        '$resource',
        'RootPathConstant',
        'PathSuffixConstant',
        function ($resource, RootPathConstant, PathSuffixConstant) {
            return $resource(RootPathConstant + '/photo/album/:albumId/photos' + PathSuffixConstant + '?countPerPage=:countPerPage&offset=:offset',
                {},
                {query: {method: 'GET', isArray: false}}
            );
        }])

    .factory('PhotoService', [
        '$resource',
        'RootPathConstant',
        function ($resource, RootPathConstant) {
            return $resource(RootPathConstant + '/photo/:photoId', {},
                {
                    save: {method: 'POST', isArray: true},
                    remove: {method: 'DELETE', isArray: false},
                    edit: {method: 'PUT', isArray: false}
                }
            );
        }])

    .factory('PhotoSliderModalService', [
        'btfModal',
        function (btfModal) {
            return btfModal({
                controller: 'PhotoSliderCtrl',
                controllerAs: 'photoSliderCtrl',
                templateUrl: 'views/photo/photo_slider.html'
            });
        }])

    .factory('PhotoCollection', ['PhotoHost', function (PhotoHost) {
        function setMiniUri(photo) {
            photo.miniUri = PhotoHost + 'cdrmimg/' + photo.fileId + '?w=220&h=180';
        }

        function setUri(photo) {
            photo.uri = PhotoHost + 'drmimg/' + photo.fileId;
        }

        return function (photos) {
            this.photos = photos.results;
            this.totalCount = photos.totalCount;
            angular.forEach(this.photos, function (photo) {
                setMiniUri(photo);
                setUri(photo);
            });
        };
    }]);
